var FOCUS = "content-img1";
var page = 1;
var index=0;  //1表示从内部返回,0本页面翻页
// var tag=getParam('tag');
// if(tag.length!=0){
FOCUS = get_cookie('focus');
page  = get_cookie('page');
index =get_cookie('index');
if (FOCUS.length == 0) {
    FOCUS = "content-img1";
}
if(page.length==0){
    page=1;
}
if(index.length==0){
    index=0;
}
if(FOCUS=='zhongjiangjilu'){
    $('#zhongjiangjilu').css("background-image", 'url(../img/zhongjiangjilu_selected.png)');
}else{
    $('.'+FOCUS).css('background-image', 'url(../img/yuanchengsongjiao/focus.png)');
}
// }
var tv_no = "";
var tv_card = "";
try {
    tv_no = hardware.STB.serialNumber;
} catch (e) {
    tv_no = '033234001051700023B8000000';
}
try {
    tv_card = CA.card.serialNumber || '';
} catch (e) {
    tv_card = '9932058300001801';
}
var ids = new Array();
var activitySize = 0;
var type=0;
// var iAlert=new iAlert();
if(page==1){
    getData();
}else{
    getNextData(page);
}
document.onirkeypress = keyDown;
document.onkeydown = keyDown;
function keyDown(evt) {
    evt = (evt) ? evt : ((window.event) ? window.event : "");//兼容IE和Firefox获得keyBoardEvent对象
    var keyCode = evt.keyCode ? evt.keyCode : evt.which;
    switch (keyCode) {
        case KEY.RIGHT:
        case KEY.RIGHT2:
            switch (FOCUS) {
                case "left":
                    $('#left').css("background-image", '');
                    $('.article-one').css("background-image", 'url(../img/focus.png)');
                    FOCUS = 'article-one';
                    break;
                case "content-img1":
                    $('.content-img2').css("background-image", 'url(../img/yuanchengsongjiao/focus.png)');
                    $('.content-img1').css("background-image", '');
                    FOCUS = 'content-img2';
                    break;
                case "content-img2":
                    $('.content-img3').css("background-image", 'url(../img/yuanchengsongjiao/focus.png)');
                    $('.content-img2').css("background-image", '');
                    FOCUS = 'content-img3';
                    break;
                case "content-img4":
                    $('.content-img5').css("background-image", 'url(../img/yuanchengsongjiao/focus2.png)');
                    $('.content-img4').css("background-image", '');
                    FOCUS = 'content-img5';
                    break;
                case "content-img5":
                    $('.content-img6').css("background-image", 'url(../img/yuanchengsongjiao/focus2.png)');
                    $('.content-img5').css("background-image", '');
                    FOCUS = 'content-img6';
                    break;
                case "content-img6":
                    $('.content-img7').css("background-image", 'url(../img/yuanchengsongjiao/focus2.png)');
                    $('.content-img6').css("background-image", '');
                    FOCUS = 'content-img7';
                    break;
                case "vedio4":
                    $('.vedio5').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio4').css("background-image", '');
                    FOCUS = 'vedio5';
                    break;
                case "vedio6":
                    $('.vedio7').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio6').css("background-image", '');
                    FOCUS = 'vedio7';
                    break;
                case "vedio7":
                    $('.vedio8').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio7').css("background-image", '');
                    FOCUS = 'vedio8';
                    break;
                case "vedio8":
                    $('.vedio9').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio8').css("background-image", '');
                    FOCUS = 'vedio9';
                    break;
                case "vedio9":
                    $('.vedio10').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio9').css("background-image", '');
                    FOCUS = 'vedio10';
                    break;
                case "vedio11":
                    $('.vedio12').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio11').css("background-image", '');
                    FOCUS = 'vedio12';
                    break;
                case "vedio12":
                    $('.vedio13').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio12').css("background-image", '');
                    FOCUS = 'vedio13';
                    break;
                case "vedio13":
                    $('.vedio14').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio13').css("background-image", '');
                    FOCUS = 'vedio14';
                    break;
                case "vedio14":
                    $('.vedio15').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio14').css("background-image", '');
                    FOCUS = 'vedio15';
                    break;
                case "right":
                    index=0;
                    page++;
                    getNextData(page);
                    break;
            }
            break;
        case KEY.LEFT:
        case KEY.LEFT2:
            switch (FOCUS) {
                case "left":
                    if (page > 1) {
                        page--;
                        getPreViousData(page)
                    }
                    break;
                case "content-img3":
                    $('.content-img2').css("background-image", 'url(../img/yuanchengsongjiao/focus.png)');
                    $('.content-img3').css("background-image", '');
                    FOCUS = 'content-img2';
                    break;
                case "content-img2":
                    $('.content-img1').css("background-image", 'url(../img/yuanchengsongjiao/focus.png)');
                    $('.content-img2').css("background-image", '');
                    FOCUS = 'content-img1';
                    break;
                case "content-img5":
                    $('.content-img4').css("background-image", 'url(../img/yuanchengsongjiao/focus2.png)');
                    $('.content-img5').css("background-image", '');
                    FOCUS = 'content-img4';
                    break;
                case "content-img6":
                    $('.content-img5').css("background-image", 'url(../img/yuanchengsongjiao/focus2.png)');
                    $('.content-img6').css("background-image", '');
                    FOCUS = 'content-img5';
                    break;
                case "content-img7":
                    $('.content-img6').css("background-image", 'url(../img/yuanchengsongjiao/focus2.png)');
                    $('.content-img7').css("background-image", '');
                    FOCUS = 'content-img6';
                    break;

                case "vedio10":
                    $('.vedio9').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio10').css("background-image", '');
                    FOCUS = 'vedio9';
                    break;
                case "vedio9":
                    $('.vedio8').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio9').css("background-image", '');
                    FOCUS = 'vedio8';
                    break;
                case "vedio8":
                    $('.vedio7').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio8').css("background-image", '');
                    FOCUS = 'vedio7';
                    break;
                case "vedio7":
                    $('.vedio6').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio7').css("background-image", '');
                    FOCUS = 'vedio6';
                    break;


                case "vedio15":
                    $('.vedio14').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio15').css("background-image", '');
                    FOCUS = 'vedio14';
                    break;
                case "vedio14":
                    $('.vedio13').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio14').css("background-image", '');
                    FOCUS = 'vedio13';
                    break;
                case "vedio13":
                    $('.vedio12').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio13').css("background-image", '');
                    FOCUS = 'vedio12';
                    break;
                case "vedio12":
                    $('.vedio11').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio12').css("background-image", '');
                    FOCUS = 'vedio11';
                    break;

                case "right":
                    $('#right').css("background-image", '');
                    $('.article-three').css("background-image", 'url(../img/focus.png)');
                    FOCUS = 'article-three';
                    break;
            }
            break;
        case KEY.UP:
        case KEY.UP2:
            switch (FOCUS) {
                case "right-detail5":
                    $('.right-detail5').css("background-image", '');
                    $('.right-detail4').css("background-image", 'url(../img/kangfu/kangfu_right.png)');
                    FOCUS = 'right-detail4';
                    break;

                case "content-img4":
                    $('.content-img3').css("background-image", 'url(../img/yuanchengsongjiao/focus.png)');
                    $('.content-img4').css("background-image", '');
                    FOCUS = 'content-img3';
                    break;

                case "li-two":
                    $('.li-two').css("background-image", '');
                    $('.li-one').css("background-image", 'url(../img/kangfu/kangfu_left.png)');
                    FOCUS = 'li-one';
                    break;
                case "vedio6":
                    $('.vedio5').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio6').css("background-image", '');
                    FOCUS = 'vedio5';
                    break;
                case "vedio11":
                    $('.vedio10').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio11').css("background-image", '');
                    FOCUS = 'vedio10';
                    break;
            }
            break;
        case KEY.DOWN:
        case KEY.DOWN2:
            switch (FOCUS) {
                case "zhongjiangjilu":
                    $('#zhongjiangjilu').css("background-image", 'url(../img/zhongjiangjilu_unselected.png)');
                    $('.article-one').css("background-image", 'url(../img/focus.png)');
                    FOCUS = 'article-one';
                    break;
                case "content-img3":
                    $('.content-img4').css("background-image", 'url(../img/yuanchengsongjiao/focus2.png)');
                    $('.content-img3').css("background-image", '');
                    FOCUS = 'content-img4';
                    break;
                case "vedio10":
                    $('.vedio11').css("background-image", 'url(../img/kechengdianbo/kcdb_bg.png)');
                    $('.vedio10').css("background-image", '');
                    FOCUS = 'vedio11';
                    break;
                case "right-detail5":
                    $('.right-detail6').css("background-image", '');
                    $('.right-detail5').css("background-image", 'url(../img/kangfu/kangfu_right.png)');
                    FOCUS = 'right-detail6';
                    break;
                case "content-right-detail4":
                    $('.content-right-detail5').css("background-image", 'url(../img/zhinan/bszl_right.png)');
                    $('.content-right-detail4').css("background-image", '');
                    FOCUS = 'content-right-detail5';
                    break;
            }
            break;
        case KEY.ENTER:
            switch (FOCUS) {
                case "zhongjiangjilu":
                    window.location.href = "record.html?tv_no=" + tv_no + "&tv_card=" + tv_card+"&tag=1";
                    setCookie('focus','zhongjiangjilu');
                    setCookie('page',page);
                    setCookie('index','1');
                    ids = null;
                    break;
                case "content-img1":

                    window.location.href = "../html/kechegndianbo.html";
                    // var ss = document.getElementById('img0').src;
                    // if (ss == ADDRESS + PORT + '/answer/img/timg.jpg') {
                    //     window.location.href = 'jqqd.html?tag=1"';
                    // } else {
                    //     window.location.href = 'detail1.html?base_id=' + ids[0] + "&tv_no=" + tv_no + "&tv_card=" + tv_card+"&tag=1";
                    // }
                    // setCookie('focus','article-one');
                    // setCookie('page',page);
                    // setCookie('index','1');
                    // ids = null;
                    break;
                case "content-img2":
                    window.location.href = "../html/zaixinzhibo.html";
                    // var ss = document.getElementById('img1').src;
                    // if (ss == ADDRESS + PORT + '/answer/img/timg.jpg') {
                    //     window.location.href = 'jqqd.html?tag=1';
                    // } else {
                    //     window.location.href = 'detail1.html?base_id=' + ids[1] + "&tv_no=" + tv_no + "&tv_card=" + tv_card+"&tag=1";
                    // }
                    // setCookie('focus','article-two');
                    // setCookie('page',page);
                    // setCookie('index','1');
                    // ids = null;
                    break;
                case "content-img3":
                    window.location.href = "../html/xuexijilv.html";
                    // var ss = document.getElementById('img2').src;
                    // if (ss == ADDRESS + PORT + '/answer/img/timg.jpg') {
                    //     window.location.href = 'jqqd.html?tag=1';
                    // } else {
                    //     window.location.href = 'detail1.html?base_id=' + ids[2] + "&tv_no=" + tv_no + "&tv_card=" + tv_card+"&tag=1";
                    // }
                    // setCookie('focus','article-three');
                    // setCookie('page',page);
                    // setCookie('index','1');
                    // ids = null;
                    break;
                case "left":
                    //console.log(page);
                    if (page > 1) {
                        page--;
                        getPreViousData(page)
                    }

                    break;
                case "right":
                    index=0;
                    page++;
                    getNextData(page);
                    break;
                case "error":
                    if (type == 0) {
                        doOk();
                    } else {
                        iAlert.close();
                    }
                    FOCUS="right";
                    break;
            }
            break;
        case KEY.EXIT:
        case KEY.BACK:
            if (typeof(Android) != "undefined") { // 安卓盒子
                iPanel.eventFrame.openPortal();
            } else
                window.location.href = "http://localhost/portalrouting/pr.do?stbid="+tv_no;
            return 0;
            break;
    }
}
function getData() {

    var ajax = null;
    if (window.XMLHttpRequest) {
        ajax = new XMLHttpRequest();
    } else {
        ajax = new ActiveXObject('Microsoft.XMLHTTP');
    }
    ajax.onreadystatechange = function () {
        if (ajax.readyState === 4 && ajax.status === 200) {
            var text = ajax.responseText;
            if (text.length != 0) {
                var response = eval("(" + text + ")");
            }

            var str = '';
            if(0 < response.zimu.length){
                //如果有多条启用的字幕，显示最新的那条。
                str="<marquee  direction='left' scrollamount='3'><span id='pmd'>"+response.zimu[response.zimu.length-1].content+"</span></marquee>";
            }else{
                str = "<marquee  direction='left' scrollamount='3'><span id='pmd'>开心抽奖火热进行中</span></marquee>";
            }

            $('#text').html(str);
            activitySize = response.artres.length;
            if (response.artres.length > 0) {
                for (var i = 0; i < response.artres.length; i++) {
                    $('#img' + i).attr('src', IMAGE + response.artres[i].activity_img);
                    ids[i] = response.artres[i].base_id;
                }
                if(response.artres.length==1){
                    $('#img1').attr('src', '../img/timg.jpg');
                    $('#img2').attr('src', '../img/timg.jpg');
                }
                if(response.artres.length==2){
                    $('#img2').attr('src', '../img/timg.jpg');
                }
            }
            if(index==0){
                FOCUS = "tu1";
                $('#article-one').css("background-image", 'url(../img/selected.png)');
            }
            ajax = null;
        }
    }
    // console.log("==================>"+STATICURL)
    // ajax.open('post', STATICURL + "Activity/lst", true);
    // ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // ajax.send('tv_no=' + tv_no + '&tv_card=' + tv_card);

    // $.ajax({
    //     url: STATICURL+"Activity/lst",
    //     type: "POST",
    //     dataType: 'json',
    //     data: {
    //         tv_no: tv_no,
    //         tv_card: tv_card,
    //     },
    //     success: function (response) {
    //         console.log(response);
    //         if(response.artres.length>0){
    //             for(var i=0;i<response.artres.length;i++){
    //                 $('#img'+i).attr('src',IMAGE+response.artres[i].activity_img);
    //                 ids[i]=response.artres[i].base_id;
    //             }
    //         }
    //     },
    //     complete:function (XHR, TS) {
    //         XHR=null;
    //     }
    // })
}
function getNextData(tpage) {
    var ajax = null;
    if (window.XMLHttpRequest) {
        ajax = new XMLHttpRequest();
    } else {
        ajax = new ActiveXObject('Microsoft.XMLHTTP');
    }
    ajax.onreadystatechange = function () {
        if (ajax.readyState == 4 && ajax.status == 200) {
            var text = ajax.responseText;
            if (text.length != 0) {
                var response = eval("(" + text + ")");
            }
            activitySize = response.artres.length;
            if (response.artres.length > 0) {
                // for (var i = 0; i < 3; i++) {
                //     $('#img' + i).attr('src', '../img/timg.jpg');
                // }
                if (response.artres.length < 3) {
                    $('#right').css('display', 'none');
                }
                for (var i = 0; i < response.artres.length; i++) {
                    $('#img' + i).attr('src', IMAGE + response.artres[i].activity_img);
                    ids[i] = response.artres[i].base_id;
                }
                if(response.artres.length==1){
                    $('#img1').attr('src', '../img/timg.jpg');
                    $('#img2').attr('src', '../img/timg.jpg');
                }
                if(response.artres.length==2){
                    $('#img2').attr('src', '../img/timg.jpg');
                }
                $('#left').css('display', 'block');
                $('#left').css('background-image', 'url(../img/left_unselected.png)');
                $('#right').css('background-image', 'url(../img/right_unselected.png)');
                if(index==0){
                    FOCUS = "article-one";
                    $('#article-one').css("background-image", 'url(../img/selected.png)');
                }
            } else {
                page--;
                if (typeof (Android) != "undefined") {
                    iAlert.hint = "<div style='font-size:25px;margin-top:50px;'>没有更多活动了<div>";
                    iAlert.open();
                    type=1;
                } else {
                    alert('没有更多活动了');
                    type=0;
                }
                FOCUS="error";
            }
            ajax = null;
        }
    }
    ajax.open('post', STATICURL + "Activity/next", true);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.send('tv_no=' + tv_no + '&tv_card=' + tv_card + '&page=' + tpage);

}
function getPreViousData(tpage) {

    if (tpage == 1) {
        $('#left').css('display', 'none');
    }
    $('#right').css('background-image', 'url(../img/right_unselected.png');
    var ajax = null;
    if (window.XMLHttpRequest) {
        ajax = new XMLHttpRequest();
    } else {
        ajax = new ActiveXObject('Microsoft.XMLHTTP');
    }
    ajax.onreadystatechange = function () {
        if (ajax.readyState == 4 && ajax.status == 200) {
            var text = ajax.responseText;
            if (text.length != 0) {
                var response = eval("(" + text + ")");
            }
            activitySize = response.artres.length;
            if (response.artres.length > 0) {
                if (response.artres.length == 3) {
                    $('#right').css('display', 'block');
                }
                for (var i = 0; i < response.artres.length; i++) {
                    $('#img' + i).attr('src', IMAGE + response.artres[i].activity_img);
                    ids[i] = response.artres[i].base_id;
                }
                if(response.artres.length==1){
                    $('#img1').attr('src', '../img/timg.jpg');
                    $('#img2').attr('src', '../img/timg.jpg');
                }
                if(response.artres.length==2){
                    $('#img2').attr('src', '../img/timg.jpg');
                }
                $('#right').css('display', 'block');
                FOCUS = "tu3";
                $('#tu3').css("background-image", 'url(../img/selected.png)');
                $('#left').css('background-image', 'url(../img/left_unselected.png)');
            }
            ajax = null;
        }
    }
    ajax.open('post', STATICURL + "Activity/previous", true);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.send('tv_no=' + tv_no + '&tv_card=' + tv_card + '&page=' + tpage);
}
function get_cookie(Name) {
    var search = Name + "="//查询检索的值
    var returnvalue = "";//返回值
    if (document.cookie.length > 0) {
        sd = document.cookie.indexOf(search);
        if (sd != -1) {
            sd += search.length;
            end = document.cookie.indexOf(";", sd);
            if (end == -1)
                end = document.cookie.length;
            //unescape() 函数可对通过 escape() 编码的字符串进行解码。
            returnvalue = unescape(document.cookie.substring(sd, end))
        }
    }
    return returnvalue;
}
function setCookie(name,value) {
    document.cookie = name + "="+ escape (value) ;
}

